package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.WemediaConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    @Autowired
    private CacheService cacheService;


    @Override
    public ResponseResult findList(SensitiveDto sensitiveDto) {

        Page<WmSensitive> page = new Page<>(sensitiveDto.getPage(),sensitiveDto.getSize());
        String name = sensitiveDto.getName();
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), WmSensitive::getSensitives, name)
                .orderByDesc(WmSensitive::getCreatedTime);

        this.page(page, queryWrapper);

        PageResponseResult pageResponseResult = new PageResponseResult(page);
        return pageResponseResult;
    }

    @Override
    public ResponseResult add(WmSensitive wmSensitive) {
        wmSensitive.setCreatedTime(new Date());
        this.save(wmSensitive);

        //添加到缓存中
        cacheService.sAdd(WemediaConstants.CACHE_KEY_SECSITIVE, wmSensitive.getSensitives());


        return ResponseResult.okResult("添加敏感词成功");
    }

    @Override
    public ResponseResult updateWithRedis(WmSensitive wmSensitive) {

        WmSensitive sensitive = this.getById(wmSensitive);
        String sensitives = sensitive.getSensitives();
        if (cacheService.sIsMember(WemediaConstants.CACHE_KEY_SECSITIVE, sensitives)) {
            cacheService.sRemove(WemediaConstants.CACHE_KEY_SECSITIVE, sensitives);
        }

        this.updateById(wmSensitive);
        cacheService.sAdd(WemediaConstants.CACHE_KEY_SECSITIVE, wmSensitive.getSensitives());



        return ResponseResult.okResult("修改敏感词成功");
    }

    @Override
    public ResponseResult deleteWithRedis(Integer id) {
        WmSensitive wmSensitive = this.getById(id);
        String sensitives = wmSensitive.getSensitives();
        if (cacheService.sIsMember(WemediaConstants.CACHE_KEY_SECSITIVE, sensitives)) {
            cacheService.sRemove(WemediaConstants.CACHE_KEY_SECSITIVE, sensitives);
        }

        this.removeById(id);
        return ResponseResult.okResult("删除成功");
    }
}
